/*
 * LabelBoxControls.java
 *
 * 
 */

package labeledbox;

import java.awt.*;
import javax.swing.Box;
import javax.swing.JColorChooser;
import javax.swing.JLabel;


public class LabelBoxControls extends javax.swing.JPanel {
    
    // Variables declaration
    static final int COLOR_CHIP_WIDTH = 40;
    static final int COLOR_CHIP_HEIGHT = 20;
    
    RectShape colorChip = new RectShape();
    CanvasShape drawingEditor;

    private CanvasShape colorButton;
    private javax.swing.JButton createBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField labelBox;
    private javax.swing.JTextField leftBox;
    private javax.swing.JTextField topBox;
    private JLabel panel_label = new JLabel("Labeled Box Maker");
    private JLabel color_label = new JLabel("Color Chooser:");
    // End of variables declaration


    // Constructor
    public LabelBoxControls(CanvasShape c) {  // The type of this arg in the call is "DrawPanel"
      drawingEditor = c;
      
      panel_label.setFont(new Font("sans-serif", Font.BOLD, 20));

      initComponents();
    } // end constructor
    
    /** This method is called from within the constructor to initialize the form.
     * 
     */

    private void initComponents() {
      
      colorButton = new CanvasShape();
      jLabel1 = new javax.swing.JLabel();
      jLabel2 = new javax.swing.JLabel();
      leftBox = new javax.swing.JTextField();
      topBox = new javax.swing.JTextField();
      jLabel3 = new javax.swing.JLabel();
      labelBox = new javax.swing.JTextField();
      createBox = new javax.swing.JButton();


      colorChip.setWidth(COLOR_CHIP_WIDTH);
      colorChip.setHeight(COLOR_CHIP_HEIGHT);
      colorButton.add(colorChip);
      colorButton.setPreferredSize(new Dimension(COLOR_CHIP_WIDTH, COLOR_CHIP_HEIGHT));

      colorButton.addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(java.awt.event.MouseEvent evt) {
              colorButtonActionPerformed(evt);
          }
      });

      jLabel1.setText("Left:");
      jLabel2.setText("Top:");

      leftBox.setColumns(4);
      leftBox.setText("0");
      leftBox.setName("leftBox");

      topBox.setColumns(4);
      topBox.setText("0");

      topBox.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              topBoxActionPerformed(evt);
          }
      });

      jLabel3.setText("Label:");

      labelBox.setColumns(20);

      createBox.setText("New Box");
      createBox.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              createBoxActionPerformed(evt);
          }
      });


      // LAYOUT

      leftBox.setMaximumSize(new Dimension(45, 25));
      topBox.setMaximumSize(new Dimension(45, 25));
      labelBox.setMaximumSize(new Dimension(225, 25));


      Box outerBox = Box.createVerticalBox();

      // "Labeled Box Maker"
      outerBox.add(Box.createRigidArea(new Dimension(0, 20)));
      outerBox.add(panel_label);

      outerBox.add(Box.createRigidArea(new Dimension(0, 20)));
      // Color swatch & label
      Box colorBox = Box.createHorizontalBox();
      colorBox.add(color_label);
      colorBox.add(Box.createRigidArea(new Dimension(10, 0)));
      colorBox.add(colorButton);
      outerBox.add(colorBox);

      // "Left:" & left textbox
      outerBox.add(Box.createRigidArea(new Dimension(0, 15)));
      Box innerBox = Box.createHorizontalBox();
      innerBox.add(jLabel1);
      innerBox.add(Box.createRigidArea(new Dimension(10, 0)));
      innerBox.add(leftBox);
      outerBox.add(innerBox);

      // "Top:" & top textbox
      outerBox.add(Box.createRigidArea(new Dimension(0, 15)));
      Box topFieldsBox = Box.createHorizontalBox();
      topFieldsBox.add(jLabel2);
      topFieldsBox.add(Box.createRigidArea(new Dimension(10, 0)));
      topFieldsBox.add(topBox);
      outerBox.add(topFieldsBox);

      // "Label:" & label textbox
      outerBox.add(Box.createRigidArea(new Dimension(0, 15)));
      Box labelFieldsBox = Box.createHorizontalBox();
      labelFieldsBox.add(jLabel3);
      labelFieldsBox.add(Box.createRigidArea(new Dimension(10, 0)));
      labelFieldsBox.add(labelBox);
      outerBox.add(labelFieldsBox);

      outerBox.add(Box.createRigidArea(new Dimension(0, 15)));

      outerBox.add(createBox);

      add(outerBox);

    } /////// end initComponents() ////////////

    
    // Event Handler for when user clicks on color swatch
    private void colorButtonActionPerformed(java.awt.event.MouseEvent evt) {
        Color newColor = JColorChooser.showDialog(this, "Choose Box Color", colorChip.getFillColor());
        colorChip.setFillColor(newColor);
        colorButton.repaint();
    }

    private void topBoxActionPerformed(java.awt.event.ActionEvent evt) {
    }
    // Handler to create a new LabeledBox()
    private void createBoxActionPerformed(java.awt.event.ActionEvent evt) {
        int left = Integer.parseInt(leftBox.getText());
        int top  = Integer.parseInt(topBox.getText());
        String label = labelBox.getText();

        drawingEditor.add(new LabeledBox(left, top, colorChip.getFillColor(), label));
        drawingEditor.repaint();
    }
    
    
} ////////////////////////// end class ////////////////////
